<template>
  <div class="item">
    <div class="title">{{ item.name }}</div>
    <div class="manager">{{ item.manager }}</div>
    <el-dropdown @command="commandEvent">
      <div class="options">
        <span>操作</span>
        <i class="el-icon-arrow-down" />
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="edit"
          >编辑部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import { companyDepartmentDelete } from '@/api/departments'
export default {
  props: {
    item: {
      type: Object,
      default: () => ({}),
      required: true,
      validator: (value) => {
        return value.name
      }
    }
  },
  mounted() {
    // this.$bus.$on('sontoitem', (str) => {
    //   console.log(str)
    // })
  },
  beforeDestroy() {
    // this.$bus.$off('sontoitem')
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          // console.log('item', this.item)
          this.$bus.$emit('showAdd', true, this.item, 'add')
          break
        case 'edit':
          this.$bus.$emit('showAdd', true, this.item, 'edit')
          break
        case 'del':
          this.$confirm('您确定删除该项吗？', '提示')
            .then(async() => {
              await companyDepartmentDelete(this.item.id)
              this.$message.success('删除成功')
              this.$emit('getData')
            })
            .catch(() => {})
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.item {
  display: flex;
  align-items: center;
  .title {
    flex: 1;
  }
  .manager {
    width: 120px;
  }
}
</style>
